# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import MySQLdb
import MySQLdb.cursors
from scrapy.exporters import JsonItemExporter
from twisted.enterprise import adbapi


class DangdangspiderPipeline(object):
    def process_item(self, item, spider):
        return item


class JsonExporterPipeline(object):
    def __init__(self):
        self.file = open('articleexport.json', 'wb')
        self.exporter = JsonItemExporter(self.file, encoding="utf-8", ensure_ascii=False)
        self.exporter.start_exporting()

    def close_spider(self, spider):
        self.exporter.finish_exporting()
        self.file.close()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item


class MysqlTwistedPipeline(object):
    def __init__(self,dbpool):
        self.dbpool= dbpool

    @classmethod
    def from_settings(cls, settings):
        dbparms = dict(
            host=settings["MYSQL_HOST"],
            db=settings["MYSQL_DBNAME"],
            user=settings["MYSQL_USER"],
            password=settings["MYSQL_PASSWORD"],
            charset='utf8',
            cursorclass=MySQLdb.cursors.DictCursor,
            use_unicode=True,
        )
        dbpool=adbapi.ConnectionPool("MySQLdb", **dbparms)

        return cls(dbpool)

    def process_item(self, item, spider):
        query=self.dbpool.runInteraction(self.do_insert,item)
        query.addErrback(self.handle_error,item,spider)

    def handle_error(self, failure, item, spider):
        print(failure)

    def do_insert(self, cursor, item):
        insert_sql = """insert into more_book(more_book_name, more_book_author, more_book_publish, more_book_img, more_book_price, more_book_littleprice, more_book_kind, more_book_put_time, more_book_information, more_book_oldprice) 
                    VALUE (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
               """
        cursor.execute(insert_sql, (item["more_book_name"],item["more_book_author"],item["more_book_publish"],
                                    item["more_book_img"],item["more_book_price"],item["more_book_littleprice"],
                                    item["more_book_kind"],item["more_book_put_time"],item["more_book_information"],item["more_book_oldprice"]))
